﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Sql;

public partial class Manage_MageDefault : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void LoginBut_Click(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrManager"].ConnectionString;

        if (AccountTex.Text == "")
        {
            Response.Write(@"<script>alert('用户名不能为空！');</script>");
            return;
        }
        if (PswTex.Text == "")
        {
            Response.Write(@"<script>alert('密码不能为空！');</script>");
            return;
        }

        string username = AccountTex.Text;
        string password = PswTex.Text;

        string sqlString = "select * from Manager_Staff where Sno = @username and Spswd = @password";
        SqlParameter[] parameters = {
                                        new SqlParameter("@username",username),
                                        new SqlParameter("@password", password)
                                    };
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    DataSet ds = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds);
                    DataTable table = ds.Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        Response.Write(@"<script>alert('登录成功！');</script>");
                        //建立cookies
                        HttpCookie usr = new HttpCookie("ManagerUsr", username);
                        HttpCookie psw = new HttpCookie("ManagerPsw", password);
                        // 设cookie的过期时间。
                        usr.Expires = DateTime.Now.AddDays(7);
                        psw.Expires = DateTime.Now.AddDays(7);
                        Response.Cookies.Add(usr);
                        Response.Cookies.Add(psw);
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                        Response.Write(@"<script>alert('登录失败！');</script>");

                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }

        }

    }
}